package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //新增套餐
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入套餐基本信息
        setmealMapper.add(setmeal);
        //插入套餐关联的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    //套餐分页查询
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList = setmealMapper.findByPage(setmealPageQueryDTO);
        Page<Setmeal> p=(Page<Setmeal>) setmealList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    //删除套餐
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        //判断是否有起售的套餐，如果有，抛出异常
        for(Long id : ids){
            Setmeal setmeal = setmealMapper.findByStatusAndId(id);
            if(setmeal != null && setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setmealMapper.delete(ids);
        //删除套餐关联的菜品
        setmealDishMapper.deleteBySetmealId(ids);
    }

    //根据id查询套餐和菜品关系
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
       return setmeal;
    }

    //修改套餐
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐基本信息
        setmealMapper.update(setmeal);
        //删除套餐关联的菜品
        setmealDishMapper.deleteBySetmealId(Arrays.asList(setmealDTO.getId()));
        //插入套餐菜品数据
        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    ////套餐起售停售
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    //根据分类id查询套餐
    public List<Setmeal> list(Long categoryId) {
        List<Setmeal> list = setmealMapper.list( categoryId);
        return list;
    }

    //根据套餐id查询菜品
    public List<Dish> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
